<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="800" destroy-on-close @closed="$emit('closed')" :close-on-click-modal="false">
		<el-form :model="form" :rules="rules" :disabled="mode == 'show'" ref="dialogForm" label-width="110px" label-position="right">
            <el-row :gutter="1">
                <el-col :span="12">
                    <el-form-item label="项目编号" prop="project_code">
                        <el-input v-model="project_code" readonly placeholder="请选择项目" @click="checkItem"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="项目id" prop="project_id">
                        <el-input v-model="form.project_id" disabled placeholder="项目id"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-col :span="12">
                <el-form-item label="项目日期" prop="project_time">
                    <el-input v-model="project_time" disabled placeholder="项目日期"></el-input>
                </el-form-item>
            </el-col>
            <el-row :gutter="1">
                <el-col :span="12">
                    <el-form-item label="公司名称" prop="company_name">
                        <el-input v-model="company_name" disabled placeholder="公司名称"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="项目名称" prop="project_name">
                        <el-input v-model="project_name" disabled placeholder="项目名称"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row :gutter="1">
                <el-col :span="12">
                    <el-form-item label="会议类型" prop="meeting_type">
                        <el-input v-model="meeting_type" disabled placeholder="会议类型"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="应收金额" prop="collect_account">
                        <el-input v-model="form.collect_account" placeholder="应收金额"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row :gutter="1">
                <el-col :span="12">
                    <el-form-item label="已回金额" prop="return_account">
                        <el-input v-model="form.return_account" readonly placeholder="已回金额" @click="checkReferrer"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="未回款金额" prop="not_account">
                        <el-input v-model="form.not_account" readonly placeholder="未回款金额"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row :gutter="1">
                <el-col :span="12">
                    <el-form-item label="回款状态" prop="collect_status">
                        <el-select v-model="form.collect_status" placeholder="请选择回款状态" style="width:100%" disabled>
                            <el-option label="未回款" :value="1"/>
                            <el-option label="部分回款" :value="2"/>
                            <el-option label="回款完成" :value="3"/>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="未回款说明" prop="not_remark" v-if="form.collect_status == 1">
                        <el-input v-model="form.not_remark" placeholder="未回款说明"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="计划回款时间" prop="plan_time">
                        <el-date-picker v-model="form.plan_time" type="date" placeholder="计划回款时间" style="width: 100%;" :value-format="'YYYY-MM-DD'"></el-date-picker>
                    </el-form-item>
                </el-col>
            </el-row>
		</el-form>
		<template #footer>
			<el-button @click="visible = false">取 消</el-button>
			<el-button v-if="mode != 'show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
    <item-list v-if="dialog.item" ref="itemDialog" @success="itemBack" @closed="dialog.item=false"></item-list>
</template>

<script>
import itemList from '../list/itemList.vue';
export default {
    components: { itemList},
	emits: ['success', 'closed'],
	data() {
		return {
			mode: "add",
			titleMap: {
				add: '新增应收账款',
				edit: '编辑应收账款',
				show: '查看'
			},
			visible: false,
			isSaveing: false,
			//表单数据
			form: {
                id:"",
                project_id:"",
                collect_account:"",
                return_account:"",
                not_account:"",
                not_remark:"",
                collect_status:"",
                plan_time:"",
			},
            project_code: "",
            project_time: "",
            company_name: "",
            project_name: "",
            meeting_type: "",
			//验证规则
			rules: {
				project_id: [{ required: true, message: '请选择项目' }],
			},
            dialog:{
                item:false,
            },
		}
	},
	mounted() {
	},
	methods: {
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		//表单提交方法
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res = [];
					if (this.form.id) {
						res = await this.$API.project.receivable.edit.post(this.form);
					} else {
						res = await this.$API.project.receivable.add.post(this.form);
					}
					this.isSaveing = false;
					if (res.code == 1) {
						this.$emit('success', this.form, this.mode)
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", { type: 'error' })
					}
				} else {
					return false;
				}
			})
		},
        setId(data){
            this.form.customer_id = data.id
            this.custom_code = data.custom_code
        },
        projectData(data){
            this.form.project_id = data.project_id
            this.project_code = data.project_code
            this.project_time = data.project_time
            this.company_name = data.customer_name
            this.project_name = data.project_name
            this.meeting_type = data.meeting_type==1 ? "会议" : data.meeting_type==2 ? "婚礼" : data.meeting_type==3 ? "会议代收" : data.meeting_type==4 ? "会议代收+自付" : "赞助"
        },
		//表单注入数据
		setData(data) {
            this.form.id = data.id
            this.form.project_id = data.project_id
            this.form.collect_account = data.collect_account
            this.form.return_account = data.return_account
            this.form.not_account = data.not_account
            this.form.collect_status = data.collect_status
            this.form.plan_time = data.plan_time
            this.form.not_remark = data.not_remark
            this.project_code = data.project.project_code
            this.project_time = data.project.project_time
            this.company_name = data.project.customer.company_name
            this.project_name = data.project.metting
            this.meeting_type = data.project.type==1 ? "会议" : data.project.type==2 ? "婚礼" : data.project.type==3 ? "会议代收" : data.project.type==4 ? "会议代收+自付" : "赞助"
		},
        checkItem(){
            this.dialog.item = true
			this.$nextTick(() => {
				this.$refs.itemDialog.open('add')
			})
        },
        itemBack(data){
            this.form.project_id = data.id
            this.project_code = data.project_code
            this.project_time = data.project_time
            this.company_name = data.customer.company_name
            this.project_name = data.metting
            this.meeting_type = data.type==1 ? "会议" : data.type==2 ? "婚礼" : data.type==3 ? "会议代收" : data.type==4 ? "会议代收+自付" : "赞助"
        },
	}
}
</script>

<style></style>
